package cn.qf.web;

import cn.qf.pojo.Admin;
import cn.qf.service.AdminService;
import cn.qf.service.impl.AdminServiceimpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class AdminLoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //接收前台参数
        String name=req.getParameter("adminname");
        String password=req.getParameter("adminpassword");
        //调用业务层接口获取数据
        AdminService adminService=new AdminServiceimpl();//pareter:参数
        Admin admin=adminService.findAdmin(name,password);//find:找到
        
        if(admin!=null){
            HttpSession session=req.getSession();//第一次创建session会话
            session.setAttribute("admin",admin);
            //请求转发到主页
            req.getRequestDispatcher("/admmin/admin_index.jsp").forward(req,resp);
        }else{
            //提示错误数据
            //request域设置提示信息数据
            req.setAttribute("msg","用户名错误");
            //请求转发到登录页面提示
            req.getRequestDispatcher("/admin/admin_login.jsp").forward(req,resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       doGet(req, resp);
    }
}
